
def signal(*args):
    #  MtmMean_v12 指标
    df = args[0]
    n = args[1]
    factor_name = args[2]

    df['mtm'] = df['close'] / df['close'].shift(n) - 1
    df['mtm'] = df['mtm']*df['taker_buy_quote_asset_volume']/df['taker_buy_quote_asset_volume'].rolling(window=n, min_periods=1).mean()
    df[factor_name] = df['mtm'].rolling(window=n, min_periods=1).mean()

    return df


# 建议改写之前，看一下帖子说明：
# 【更新必看】中性策略框架v2.1.12更新说明 (https://bbs.quantclass.cn/thread/43067)
def signal_multi_params(df, param_list) -> dict:
    """
    使用同因子多参数聚合计算，可以有效提升回测、实盘 cal_factor 的速度，
    相对于 `signal` 大概提升3倍左右
    :param df: k线数据的dataframe
    :param param_list: 参数列表
    """
    ret = dict()
    for param in param_list:
        n = int(param)

        df['mtm'] = df['close'] / df['close'].shift(n) - 1
        df['mtm'] = df['mtm'] * df['taker_buy_quote_asset_volume'] / df['taker_buy_quote_asset_volume'].rolling(n, min_periods=1).mean()

        ret[str(param)] = df['mtm'].rolling(window=n, min_periods=1).mean()
    return ret
